package com.yy.train.ticket.tackle;

import lombok.Data;

@Data
public class TicketLinkedList {
    private final Ticket head = new Ticket(0L,0L, "", "");

    public Ticket getHead() {
        return head;
    }

    //添加节点到单向链表
    //思路，当不考虑编号顺序时
    //1. 找到当前链表的最后节点
    //2. 将最后这个节点的next 指向 新的节点
    public void add(Ticket heroNode) {
        //因为head节点不能动，因此我们需要一个辅助遍历 temp
        Ticket temp = head;
        while (true){
            if (temp.next==null){
                break;
            }
            temp=temp.next;
        }
        temp.next=heroNode;
        System.out.println(head);
    }

    public void addByOrder(Ticket heroNode) {
        //因为head节点不能动，因此我们需要一个辅助遍历 temp
        boolean flag = false; // flag标志添加的编号是否存在，默认为false
        Ticket temp = head;
        while (true){
            if (temp.next==null){
                break;
            }
            if (temp.next.startStationId>heroNode.startStationId){

                break;
            }else if(temp.next.startStationId==heroNode.startStationId){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            System.out.println("已存在");
        }else {
            Ticket temp1=temp.next;
            heroNode.next=temp1;
            temp.next=heroNode;
            //System.out.println(head);
        }
    }

    //显示链表[遍历]
    public void list() {
        Ticket heroNode1=   head.next;
        if (heroNode1==null){
            System.out.println("链表为空");
        }
        while (true){
            if (heroNode1==null){
                break;
            }
            System.out.println(heroNode1);
            heroNode1=heroNode1.next;

        }
    }

    //删除节点
    //思路
    //1. head 不能动，因此我们需要一个temp辅助节点找到待删除节点的前一个节点
    //2. 说明我们在比较时，是temp.next.no 和  需要删除的节点的no比较
    public void del(int no) {
        boolean flag = false; // flag标志添加的编号是否存在，默认为false
        Ticket temp = head;

        while (true){
            if (temp.next==null){
                break;
            }
            if(temp.next.startStationId==no){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if (flag){
            Ticket heroNode1=temp.next.next;
            temp.next=heroNode1;
            System.out.println(temp);
            System.out.println(head);
        }else {

            System.out.println("不存在");
        }
    }

}
class Ticket{
    /**
     * 开始站点
     */
    public Long startStationId;
    /**
     * 结束站点
     */
    public Long endStationId;
    public String startStation;

    public String  endStation;
    public Ticket  next; //指向下一个节点
    public Ticket(Long startStationId, Long endStationId,String startStation, String endStation) {
        this.startStationId = startStationId;
        this.endStationId = endStationId;
        this.startStation = startStation;
        this.endStation = endStation;
    }

    @Override
    public String toString() {
        return "Ticket{" +
                "startStationId=" + startStationId +
                ", endStationId=" + endStationId +
                ", startStation='" + startStation + '\'' +
                ", endStation='" + endStation + '\'' +
                ", next=" + next +
                '}';
    }
}